<template>
  <div id="centerRight1">
    <div class="bg-color-black">
      <div class="d-flex pt-2 pl-2">
        <span>
          <icon name="chart-line" class="text-icon"></icon>
        </span>
        <div class="d-flex">
          <span class="fs-xl text mx-2">勤务绩效排行榜</span>
        </div>
      </div>
      <div class="d-flex jc-center body-box">
        <dv-scroll-board class="dv-scr-board" :config="config" />
      </div>
    </div>
  </div>
</template>

<script>
import { getTaskEfficiencyRank } from '@/api/bigscreen'

export default {
  data() {
    return {
      config: {
        header: ['警员', '辖区', '完成率'],
        data: [],
        rowNum: 7, //表格行数
        headerHeight: 35,
        headerBGC: '#0f1325', //表头
        oddRowBGC: '#0f1325', //奇数行
        evenRowBGC: '#171c33', //偶数行
        index: true,
        columnWidth: [50],
        align: ['center']
      }
    }
  },
  mounted() {
    this.fetchRankData()
    // 定时刷新数据
    setInterval(() => {
      this.fetchRankData()
    }, 60000) // 每1分钟刷新一次
  },
  methods: {
    fetchRankData() {
      getTaskEfficiencyRank().then(res => {
        if (res.code === 200) {
          const rankData = res.data
          if (rankData && rankData.length) {
            // 格式化数据为表格所需格式
            const formattedData = rankData.map(item => {
              const completionRate = parseFloat(item.completion_rate).toFixed(0)
              const trend = completionRate >= 80 ? 'colorGrass' : 'colorRed'
              const arrow = completionRate >= 80 ? '↑' : '↓'
              return [
                item.name || '未知',
                item.department || '未知',
                `<span class='${trend}'>${arrow}${completionRate}%</span>`
              ]
            })
            
            // 更新表格数据
            this.config.data = formattedData
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$box-height: 410px;
$box-width: 300px;
#centerRight1 {
  padding: 16px;
  padding-top: 20px;
  height: $box-height;
  width: $box-width;
  border-radius: 5px;
  .bg-color-black {
    height: $box-height - 30px;
    border-radius: 10px;
  }
  .text {
    color: #c3cbde;
  }
  .body-box {
    border-radius: 10px;
    overflow: hidden;
    .dv-scr-board {
      width: 270px;
      height: 340px;
    }
  }
}
</style>
